package com.sztx.pay.center.core.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.util.StringUtil;
import com.sztx.pay.center.core.service.MqMessageService;
import com.sztx.pay.center.dataaccess.dao.redis.LockRedisDAO;
import com.sztx.se.core.mq.consumer.MqMessageListener;

/**
 * 
 * @author zhihongp
 *
 */
@Component
public class GateWayMessageListener extends MqMessageListener {
	private static final Logger log = LoggerFactory.getLogger(GateWayMessageListener.class);
	@Autowired
	private MqMessageService mqMessageService;
	
	@Autowired
	private LockRedisDAO lockRedisDAO;
	
	@Override
	public Object handleMessage(String messageId, String messageContent, String queue) {		
		MDC.remove("msgno"); //重新生成msgno
		
		log.info(messageId+"|"+messageContent+"|"+queue);
		
		if(messageId != null && !"null".equals(messageId)){
			boolean flag = lockRedisDAO.lockMqMessage(messageId);
			if (!flag) {
				log.info("mq  message "+messageId+" has handle");
				return null;
			}
		}
		
		if (StringUtil.isNotBlank(messageContent)) {
			try {
				JSONObject data = JSONObject.parseObject(messageContent);
				mqMessageService.doHandle(data);
			} catch (Exception e) {
				log.error("gate way message error.", e);
			}
		}
		return null;
	}

}
